package com.example.hxds.workflow.service.impl;
import cn.hutool.core.map.MapUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.workflow.db.dao.OrderCommentDao;
import com.example.hxds.workflow.service.OrderCommentService;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * @Author XUYANG
 * @Date 2024-04-10 15:06
 */
@Service
public class OrderCommentServiceImpl implements OrderCommentService {
    @Resource
    private TaskService taskService;

    @Resource
    private OrderCommentDao orderCommentDao;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private HistoryService historyService;

    @Transactional
    @LcnTransaction
    @Override
    public void acceptCommentAppeal(Map map) {
        HashMap orderId = this.orderCommentDao.searchCommentIdAndInstanceId(map);
        if (orderId != null && !orderId.isEmpty()) {
            String instanceId = MapUtil.getStr(orderId, "instanceId");
            if (instanceId == null){
                throw new HxdsException("没有找到工作流实例");
            }
            Task task = (Task)((TaskQuery)((TaskQuery)((TaskQuery)((TaskQuery)this.taskService.createTaskQuery().processInstanceId(instanceId)).includeProcessVariables()).includeTaskLocalVariables()).taskName("人工审核")).singleResult();
            String userId = MapUtil.getStr(map, "userId");
            this.taskService.claim(task.getId(), userId);
            if (this.orderCommentDao.acceptAppeal(map) != 1){
                throw new HxdsException("更新订单评价记录的user_id字段失败");
            }
        } else {
            throw new HxdsException("没有找到评价记录");
        }
    }
    @Override
    @Transactional
    @LcnTransaction
    public void startCommentWorkflow(Map paramMap) {
        long orderId = MapUtil.getLong(paramMap, "orderId").longValue();
        HashMap<Object, Object> var4 = new HashMap<>();
        var4.put("orderId", Long.valueOf(orderId));
        HashMap hashMap = this.orderCommentDao.searchCommentIdAndInstanceId(var4);
        if (hashMap != null && !hashMap.isEmpty()) {
            HashMap var10000 = hashMap;
            String instanceId = MapUtil.getStr(hashMap, "instanceId");
            orderId = MapUtil.getLong(var10000, "id").longValue();
            if (instanceId!=null){
                throw new HxdsException("该订单已经存在工作流实例");
            }
            paramMap.put("commentId", "" + orderId);
            String getProcessInstanceId = runtimeService.startProcessInstanceByKey("comment", paramMap).getProcessInstanceId();
            var4 = new HashMap<>();
            var4.put("commentId", Long.valueOf(orderId));
            var4.put("instanceId", getProcessInstanceId);
            if (this.orderCommentDao.startWorkflow(var4) != 1){
                throw new HxdsException("更新订单instance_id字段失败");
            }
        } else {
            throw new HxdsException("没有找到评价记录");
        }
    }
    @Override
    public HashMap searchAppealContent(String instanceId, boolean isEnd) {
        if (!isEnd) {
            ProcessInstance processInstance = (ProcessInstance)this.runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).includeProcessVariables().singleResult();
            if (processInstance == null){
                throw new HxdsException("没有找到工作实例");
            }
            Map map1 = processInstance.getProcessVariables();
            String str1 = MapUtil.getStr(map1, "reason");
            String str2 = MapUtil.getStr(map1, "note");
            HashMap<Object, Object> hashMap1 = new HashMap<>();
            hashMap1.put("reason", str1);
            hashMap1.put("note", str2);
            return hashMap1;
        }
        HistoricProcessInstance historicProcessInstance = (HistoricProcessInstance)this.historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceId).includeProcessVariables().singleResult();
        if (historicProcessInstance == null){
            throw new HxdsException("没有找到工作实例");
        }
        Map map = historicProcessInstance.getProcessVariables();
        String reason = MapUtil.getStr(map, "reason");
        String note = MapUtil.getStr(map, "note");
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("reason", reason);
        hashMap.put("note", note);
        return hashMap;
    }

    @Transactional
    @LcnTransaction
    @Override
    public void handleCommentAppeal(Map paramMap) {
        String orderId = MapUtil.getStr(paramMap, "instanceId");
        String var3 = MapUtil.getStr(paramMap, "userId");
        Task task = (Task)((TaskQuery)((TaskQuery)((TaskQuery)((TaskQuery)((TaskQuery)this.taskService.createTaskQuery().processInstanceId(orderId)).includeProcessVariables()).includeTaskLocalVariables()).taskName("人工审核")).taskAssignee(var3)).singleResult();
        if (task == null){
            throw new HxdsException("没有找到审批任务");
        }
        var3 = MapUtil.getStr(paramMap, "result");
        String var4 = MapUtil.getStr(paramMap, "reason");
        var4 = MapUtil.getStr(paramMap, "note");
        this.taskService.setVariable(task.getId(), "result", var3);
        this.taskService.setVariable(task.getId(), "note", var4);
        paramMap.remove("instanceId");
        int var6 = "同意".equals(var3) ? 4 : 3;
        paramMap.put("status", Integer.valueOf(var6));
        if (this.orderCommentDao.updateStatus(paramMap) != 1){
            throw new HxdsException("更新评价状态失败");
        }
        this.taskService.complete(task.getId());
    }
}

